package org.techmore.hakka;

import org.techmore.bussiness.ChallengeMainActivityDataHandler;
import org.techmore.bussiness.QuestionsBeanDataHandler;
import org.techmore.common.utils.activity.ActivityUtils;
import org.techmore.common.utils.sound.SoundPoolUtils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.LinearLayout.LayoutParams;

public class ChallengeMainActivity extends Activity {
	
	private PopupWindow mainPopupWindow = null;
	private LinearLayout mainactivityid = null;
	
	private Button challenge_button = null;
	
	private Handler questionsHandler = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		ActivityUtils.getInstance().setScreenOrientation(this);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.challenge_main);
		
		challenge_button = (Button) findViewById(R.id.challenge_button);
		mainactivityid = (LinearLayout) findViewById(R.id.mainactivityid);
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		if(null != mainPopupWindow){
			mainPopupWindow.dismiss();
		}
		
		questionsHandler = new Handler(Looper.myLooper()){
			public void handleMessage(android.os.Message msg) {
				if(msg.what == 1){
					
					try {
						mainPopupWindow.dismiss();
					} catch (Exception e) {
						e.printStackTrace();
					}
					//将总分置为0；
					QuestionsBeanDataHandler.getInstance().refreshTotalScore();
					//置索引；
					QuestionsBeanDataHandler.getInstance().refreshIndex();
					
					ActivityUtils.getInstance().leftIN_rightOUT_Transition(ChallengeMainActivity.this, ChallengeMainActivity.this, AnswerQuestionActivity.class);
				}
			};
		};
		
		challenge_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SoundPoolUtils.getInstance(ChallengeMainActivity.this).playSound(SoundPoolUtils.SOUND_BUTTON_CLICK);
				initProcessDialog();
				mainPopupWindow.showAtLocation(mainactivityid, Gravity.CENTER, 0, 0);
				questionsHandler.postDelayed(new InitQuestionData(questionsHandler,ChallengeMainActivity.this),1500);
			}
		});
		
	}
	
	
	private class InitQuestionData implements Runnable{
		private Handler handler;
		private Context context;
		public InitQuestionData(Handler handler, Context context) {
			this.handler = handler;
			this.context = context;
		}
		@Override
		public void run() {
			ChallengeMainActivityDataHandler.getInstance().initQuestionList(context,handler);
		}
	}
	
	
	
	private void initProcessDialog(){
		View view = LayoutInflater.from(this).inflate(R.layout.select_questions, null);
//		view.getBackground().setAlpha(120); //0-255
		mainPopupWindow = new PopupWindow(view,LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		mainPopupWindow.setAnimationStyle(R.style.PopupAnimation);
	}
	
}
